import { PageDto } from "../models/page.model";
import { UaVideoSearchDto, UaVideoEntity } from "../models/ua-card.model";
import { GlobalService } from "./global.service";
import { LocalStorage } from "./local-storage.service";


export class UaVideoService {
  static PATH = "/ua/video";

  static getBiliVideoInfo(url: string) {
    return new Promise<UaVideoEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaVideoService.PATH}/getBiliVideoInfo`,
          method: "POST",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data: {
            url
          },
          success: (response) => resolve(response.data as UaVideoEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static searchWithPage(data: UaVideoSearchDto, page: number) {
    return new Promise<PageDto<UaVideoEntity>>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaVideoService.PATH}/search?page=${page}`,
          method: "POST",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data,
          success: (response) => resolve(response.data as PageDto<UaVideoEntity>),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static findById(id: number) {
    return new Promise<UaVideoEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaVideoService.PATH}/${id}`,
          method: "GET",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as UaVideoEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static hot(id: number) {
    return new Promise<boolean>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaVideoService.PATH}/hot/${id}`,
          method: "GET",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as any),
          fail: (err) => reject(err)
        });
      }
    )
  }


  static view(id: number) {
    return new Promise<boolean>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaVideoService.PATH}/view/${id}`,
          method: "GET",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as any),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static removeById(id: number) {
    return new Promise<UaVideoEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaVideoService.PATH}/${id}`,
          method: "DELETE",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as UaVideoEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static createVideo(data: UaVideoEntity) {
    return new Promise<UaVideoEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaVideoService.PATH}/`,
          method: "POST",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data,
          success: (response) => resolve(response.data as UaVideoEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static updateVideo(data: UaVideoEntity) {
    return new Promise<UaVideoEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaVideoService.PATH}/`,
          method: "PUT",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data,
          success: (response) => resolve(response.data as UaVideoEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }
}
